package com.android.iridium;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import org.osmdroid.DefaultResourceProxyImpl;
import org.osmdroid.api.IGeoPoint;
import org.osmdroid.events.MapListener;
import org.osmdroid.events.ScrollEvent;
import org.osmdroid.events.ZoomEvent;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.ItemizedIconOverlay.OnItemGestureListener;
import org.osmdroid.views.overlay.ItemizedOverlay;
import org.osmdroid.views.overlay.ItemizedOverlayWithFocus;
import org.osmdroid.views.overlay.MyLocationOverlay;
import org.osmdroid.views.overlay.OverlayItem;



public class IridiumMapActivity  extends Activity {


	    private static final String PREFS_NAME = "MAP_PREFS";
		private DefaultResourceProxyImpl mResourceProxy;
		private MyLocationOverlay mMyLocationOverlay;

		@Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
	        setContentView(R.layout.map);
	        MapView mapView = (MapView) findViewById(R.id.simple_map);
	        mapView.setClickable(true);
	        mapView.setBuiltInZoomControls(true);
	        mapView.setTileSource(TileSourceFactory.CYCLEMAP);
	        mapView.setMultiTouchControls(true);
	        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
	        int lastLat = settings.getInt("lastLat", (int) (50.3 * 1E6));
	        int lastLon = settings.getInt("lastLon", (int)(30.5 * 1E6));
	        int lastZoom = settings.getInt("lastZoom", 5);
	        mapView.getController().setZoom(lastZoom);


	        GeoPoint center = new GeoPoint( lastLat,  lastLon); 
	        mapView.getController().setCenter(center);

	        final MapController mOsmvController = mapView.getController();
	        
	        mMyLocationOverlay = new MyLocationOverlay(this, mapView,
	        		mResourceProxy);
	        mMyLocationOverlay.enableMyLocation(); // not on by default
	        mMyLocationOverlay.enableCompass();
	        mMyLocationOverlay.disableFollowLocation();
	        mMyLocationOverlay.setDrawAccuracyEnabled(true);
	        mMyLocationOverlay.runOnFirstFix(new Runnable() {
	            public void run() {
	            	mOsmvController.animateTo(mMyLocationOverlay
	                        .getMyLocation());
	            }
	        });

	        mapView.getOverlays().add(mMyLocationOverlay);
	        mapView.invalidate();
	        
	        
	        //First create a list of items:
	        	ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();

	        	//Then add items to the list:
	        	OverlayItem overlayItem = new OverlayItem("name", "description", center);
	        	items.add(overlayItem);

	        	//Put the list in your new overlay:
	        	ItemizedOverlayWithFocus<OverlayItem> placesLocationOverlay = 
	        			new ItemizedOverlayWithFocus<OverlayItem>(
	        					getBaseContext(), items,
	        					new OnItemGestureListener<OverlayItem>() {
	        						@Override
	        						public boolean onItemSingleTapUp(final int index,
	        								final OverlayItem item) {
	        							// TODO
	        							return true; // We 'handled' this event.
	        						}

	        						@Override
	        						public boolean onItemLongPress(final int index,
	        								final OverlayItem item) {
	        							// TODO
	        							return true; // We 'handled' this event.
	        						}
	        					});

	        	//And finally add the overlay to your map:
	        	mapView.getOverlays().add(placesLocationOverlay);
	        	mapView.invalidate();

		        	
	        	mapView.setMapListener(new MapListener(){

					@Override
					public boolean onScroll(ScrollEvent arg0) {
						
						MapView mapView = (MapView) findViewById(R.id.simple_map);
				        //First create a list of items:
			        	ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();

			        	//Then add items to the list:
			        	OverlayItem overlayItem = new OverlayItem("name", "description",  (GeoPoint)mapView.getMapCenter());
			        	items.add(overlayItem);

			        	//Put the list in your new overlay:
			        	ItemizedOverlayWithFocus<OverlayItem> placesLocationOverlay = 
			        			new ItemizedOverlayWithFocus<OverlayItem>(
			        					getBaseContext(), items,
			        					new OnItemGestureListener<OverlayItem>() {
			        						@Override
			        						public boolean onItemSingleTapUp(final int index,
			        								final OverlayItem item) {
			        							// TODO
			        							return true; // We 'handled' this event.
			        						}

			        						@Override
			        						public boolean onItemLongPress(final int index,
			        								final OverlayItem item) {
			        							// TODO
			        							return true; // We 'handled' this event.
			        						}
			        					});
						
			        	//And finally add the overlay to your map:
			        	mapView.getOverlays().set(1, placesLocationOverlay);
			        	mapView.invalidate();
						return false;
					}

					@Override
					public boolean onZoom(ZoomEvent arg0) {
						// TODO Auto-generated method stub
						return false;
					}
	        	
	        	});
	        	
	    }
	    
		protected boolean isRouteDisplayed() {
			return false;
		}
		
		public void onStop(){
			 MapView mapView = (MapView) findViewById(R.id.simple_map);
			SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
	        SharedPreferences.Editor editor = settings.edit();
	        GeoPoint center = (GeoPoint)mapView.getMapCenter();
	        editor.putInt("lastLat", center.getLatitudeE6());
	        editor.putInt("lastLon", center.getLongitudeE6());
	        editor.putInt("lastZoom", mapView.getZoomLevel());
	        editor.commit();
	        
	        
	        
	        super.onStop();
		}
		
		public void finish(){
			MapView mapView = (MapView) findViewById(R.id.simple_map);
			 GeoPoint center = (GeoPoint)mapView.getMapCenter();
			Intent intent = getIntent();
			intent.putExtra("lat",center.getLatitudeE6() );
			intent.putExtra("lon", center.getLongitudeE6());
			
			setResult(RESULT_OK, intent);
			super.finish();
		}
	
}
